package cn.oms.oms.service.impl;

import cn.oms.oms.entity.Bill;
import cn.oms.oms.entity.Patient;
import cn.oms.oms.mapper.BillMapper;
import cn.oms.oms.mapper.PatientMapper;
import cn.oms.oms.service.IBillService;
import cn.oms.oms.vo.BillVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@Service
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements IBillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private PatientMapper patientMapper;

    /**
     * 根据查询条件查询账单列表
     * @param billVo
     * @return
     */
    @Override
    public PageInfo<Bill> selectBillList(BillVo billVo) {
        QueryWrapper<Bill> query = new QueryWrapper<>();
        List<Integer> patientIdList = new ArrayList<>();
        if (StringUtil.isNotEmpty(billVo.getPatientName())){
            // 根据患者名称查找患者ID
            QueryWrapper<Patient> query1 = new QueryWrapper<>();
            query1.like("patientName",billVo.getPatientName());
            query1.eq("deleteFlag",0);
            List<Patient> patientList = patientMapper.selectList(query1);
            if (patientList.size()>0&&patientList!=null){
                for (Patient patient:patientList){
                    patientIdList.add(patient.getPatientId());
                }
            }
            // 患者ID去重
            patientIdList = patientIdList.stream().distinct().collect(Collectors.toList());
            query.eq("patientId",billVo.getPatientName())
                    .or().in("patientId",patientIdList);
        }

        if (billVo.getBillPayFlag()!=null&&!"".equals(billVo.getBillPayFlag())){
            query.eq("billPayFlag",billVo.getBillPayFlag());
        }
        query.eq("deleteFlag",0);
        PageHelper.startPage(billVo.getPageNum(),billVo.getPageSize());

        List<Bill> billList = billMapper.selectList(query);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 数据格式化
        for (Bill bill:billList){
            bill.setReturnCreateTime(format.format(bill.getCreateTime()));
            QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("patientId",bill.getPatientId());
            queryWrapper.eq("deleteFlag",0);
            Patient patient = patientMapper.selectOne(queryWrapper);
            if (patient!=null){
                bill.setPatientName(patient.getPatientName());
            }
        }

        return new PageInfo<>(billList);
    }

    /**
     * 新增账单
     * @param billVo
     * @return
     */
    @Override
    public String addBill(BillVo billVo) {
        return null;
    }

    /**
     * 修改账单
     * @param billVo
     * @return
     */
    @Override
    public String updateBill(BillVo billVo) {
        return null;
    }

    /**
     * 删除账单
     * @param id
     * @return
     */
    @Override
    public String deleteBill(Integer id) {
        return null;
    }
}
